package com.atguigu.eduService.service.impl;

import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.mapper.EduTeacherMapper;
import com.atguigu.eduService.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 亚当
 * @since 2022-04-24
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherService teacherService;

    //查询前4条名师
    @Cacheable(value = "teacher",key = "'getTeacher'")
    @Override
    public List<EduTeacher> getList(QueryWrapper<EduTeacher> wrapperTeacher) {
        List<EduTeacher> teacherList = teacherService.list(wrapperTeacher);
        return teacherList;
    }

    //分页查询讲师方法
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> eduTeacherPage) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据分装到pageTeacher对象
        baseMapper.selectPage(eduTeacherPage,wrapper);
        //把分页数据获取出来，放到map集合
        List<EduTeacher> records = eduTeacherPage.getRecords();
        long current = eduTeacherPage.getCurrent();
        long pages = eduTeacherPage.getPages();
        long size = eduTeacherPage.getSize();
        long total = eduTeacherPage.getTotal();
        boolean hasNext = eduTeacherPage.hasNext(); //是否有下一页
        boolean hasPrevious = eduTeacherPage.hasPrevious(); //是否有上一页
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }
}
